import { getListApi as getAllCountryApi } from "@/api/configCenter/countryRegion";
import { getListApi as getAllMaterialApi } from "@/api/configCenter/material";
import { getTttxAllServiceApi } from "@/api/configCenter/warehouseEntryChannel";
import { getListApi as getChannelApi } from "@/api/configCenter/outBoundAgent";
import { getCustomerSelectList } from "@/api/managementCenter/customerManagement";
import CommonHook from "@/hook/commonHook";
import { reactive, ref } from "vue";

export default function InitHook() {
  const { dataDict } = CommonHook();
  const formDataRef = ref<any>(null);
  const formState = reactive<Partial<WarehouseEntryChannel.AddForm>>({
    channelName: undefined,
    channelAllocation: undefined,
    deliveryType: undefined,
    transportType: undefined,
    taxPaymentMethod: [],
    customsDeclarationMethod: [],
    expressCompany: [],
    currency: undefined,
    materialQualityIdList: [],
    countryRegionIdList: [],
    countingRatio: undefined,
    weightCalculationMethod: undefined,
    multipleWeightingMethod: undefined,
    splitFoamRatio: undefined,
    densityRatio: undefined,
    minBoxCalculateWeight: undefined,
    minBatchBalanceWeight: undefined,
    volumeCalculateRule: undefined,
    batchBalanceCalculateRule: undefined,
    weightCalculateRule: undefined,
    poNumber: undefined,
    waybillPrefix: undefined,
    outWarehouseId: undefined, //出仓代理表id
    agentChannelName: undefined, //	代理渠道名称
    agentChannelId: undefined, //代理渠道id 也就是天图的服务id
    pushOrdersType: undefined, //0拣货代理 1天图或心一
    customerIdList: [] //可见客户
  });
  const materialOpts = ref<Array<Base.GeneraDataType>>([]);
  const countryOpts = ref<Array<Base.GeneraDataType>>([]);
  const cannelOpts = ref<Array<Base.GeneraDataType>>([]); //渠道
  const agentCannelOpts = ref<Array<Base.GeneraDataType>>([]); //代理渠道
  const pushOrderOpts = ref<Array<Base.GeneraDataType>>([]); //推单代理渠道
  const customerIdOpts = ref<Array<Base.GeneraDataType>>([]); //可见客户

  const loading = ref<boolean>(false);

  const getAllCustomerIdList = () => {
    return getCustomerSelectList({ isFilterChild: 0 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        customerIdOpts.value = data || [];
      }
    });
  };

  const getAllCountry = () => {
    return getAllCountryApi({ status: 1, pageNum: 1, pageSize: 999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        countryOpts.value = data.records || [];
      }
    });
  };

  const getChannelAgent = (e: number) => {
    getTttxAllServiceApi({ outWarehouseId: e }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        agentCannelOpts.value = data || [];
      }
    });
  };

  const getChannel = () => {
    getChannelApi({ beforePageNum: 1, pageNum: 1, pageSize: 100, total: 4, name: "", status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        cannelOpts.value = data.records || [];
      }
    });
  };

  // 获取材质
  const getAllMasterial = () => {
    return getAllMaterialApi({ pageNum: 1, pageSize: 9999999, status: 1 } as any).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        materialOpts.value = data.records || [];
      }
    });
  };

  return {
    formDataRef,
    formState,
    dataDict,
    loading,
    materialOpts,
    getAllMasterial,
    countryOpts,
    getAllCountry,
    cannelOpts,
    agentCannelOpts,
    getChannel,
    pushOrderOpts,
    getChannelAgent,
    getAllCustomerIdList,
    customerIdOpts
  };
}
